package cn.com.wzx.framework.common.result;

import cn.com.wzx.framework.common.ErrorCode;
import cn.com.wzx.framework.common.enums.GlobalErrorCodeConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @BelongsProject: agile-development
 * @BelongsPackage: cn.com.wzx.framework.common.result
 * @Author: Mr.wang
 * @CreateTime: 2024-08-09  16:44
 * @Description: 返回响应结果
 * @Version: 1.0
 */
@Data
@Accessors(chain = true)
@Getter
@Setter
@Builder
public class CommonResult<T> implements Serializable {

    /**
     * 错误码
     *
     * @see ErrorCode#getCode()
     */
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 错误提示，用户可阅读
     *
     * @see ErrorCode#getMsg() ()
     */
    private String msg;

    public CommonResult(Integer code ,T data ,String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public CommonResult(Integer code ,String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResult() {
    }

    /**
     * 返回成功
     * @param data 数据
     * @return CommonResult
     * @param <T> 类型
     */
    public static <T>  CommonResult<T> success(T data){
        return CommonResult.<T>builder().build().setCode(GlobalErrorCodeConstants.SUCCESS.getCode())
                .setData(data).setMsg(GlobalErrorCodeConstants.SUCCESS.getMsg());
    }

    /**
     * 返回未知
     * @return CommonResult
     * @param <T> 类型
     */
    public static <T>  CommonResult<T> unknown(){
        return CommonResult.<T>builder().build().setCode(GlobalErrorCodeConstants.UNKNOWN.getCode())
                .setMsg(GlobalErrorCodeConstants.UNKNOWN.getMsg());

    }


}
